import time
import threading

g_num = 100


def work_1():
    global g_num
    for i in range(3):
        # 循环相加三次
        g_num += 1
    print(f"子线程1中计算得出的值为: {g_num}")


def work_2():
    global g_num
    print(f'子线程2中获取到的值为: {g_num}')


if __name__ == '__main__':
    # 线程启动之前获取全局变量
    print(f'子线程未启动之前主线程获取的值为: {g_num}')

    # 创建线程对象
    t1 = threading.Thread(target=work_1)
    t1.start()

    # 通过延迟保证t1线程中的事情做完
    time.sleep(0.1)

    t2 = threading.Thread(target=work_2)
    t2.start()

    time.sleep(0.1)
    print(f'主线程最终获取到的值为: {g_num}')
